package com.funny.stock.data.controller;

import com.funny.stock.base.util.ResultUtil;
import com.funny.stock.base.vo.Result;
import com.funny.stock.data.collect.impl.RealTimeStockServiceDelegate;
import com.funny.stock.data.dao.entity.StockDO;
import com.funny.stock.data.service.StockService;
import com.funny.stock.data.vo.RealtimeStockData;
import com.funny.stock.data.vo.StockType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 股票实时数据处理
 *
 * @author pengl
 * @date 2020/11/09
 **/
@RestController
@RequestMapping("/stock/realtime")
public class StockRealtimeDataController {

    @Autowired
    RealTimeStockServiceDelegate realTimeStockServiceDelegate;

    @Autowired
    StockService stockService;

    /**
     * 获取股票的最后数据信息，价格，涨幅，成交数据，优先从缓存中读取，若无则去第三方数据源读取
     * @param code
     * @return
     */
    @RequestMapping(value = "/data",method = RequestMethod.GET)
    public Result getRealtimeData(@RequestParam String code){
        StockDO stockDO = stockService.findByCode(code);
        if(stockDO == null) return ResultUtil.error("股票代码不存在，请联系管理员");

        RealtimeStockData realtimeData = realTimeStockServiceDelegate.getRealtimeData(code, StockType.getValueByType(stockDO.getMarket()));

        return ResultUtil.success(realtimeData);
    }

}
